package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.AdminRoleDTO;
import com.ayf.payment.game.api.entity.platform.AdminRole;
import com.ayf.payment.game.api.entity.platform.AdminRolePermission;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.platform.AdminRoleMapper;
import com.ayf.payment.game.api.mapper.platform.AdminRolePermissionMapper;
import com.ayf.payment.game.api.service.platform.AdminRoleService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Resource
    private AdminRoleMapper adminRoleMapper;

    @Resource
    AdminRolePermissionMapper adminRolePermissionMapper;

    @Override
    public void insertRoleAndPermission(AdminRoleDTO adminRoleDTO) {
        Example example = new Example(AdminRole.class);
        example.createCriteria().andEqualTo("name",adminRoleDTO.getName());
        AdminRole db=adminRoleMapper.selectOneByExample(example);
        if(db!=null){
            throw new ParameterException("角色名已存在！");

        }
        AdminRole adminRole=new AdminRole();
        adminRole.setRoleStatus((short) 1);
        adminRole.setRemark(adminRoleDTO.getRemark());
        adminRole.setName(adminRoleDTO.getName());
        adminRole.setCreateTime(new Date());
        adminRoleMapper.insertAdminRole(adminRole);
        Integer[] permissionIds=adminRoleDTO.getPermissionIds();
        if(permissionIds!=null && permissionIds.length>0){
            List<AdminRolePermission> list=new ArrayList<>();
            for(int roleId:permissionIds){
                AdminRolePermission rolePermission=new AdminRolePermission();
                rolePermission.setRoleId(adminRole.getId());
                rolePermission.setPermissionId(roleId);
                list.add(rolePermission);
            }
            adminRolePermissionMapper.inserts(list);
        }
    }

    @Override
    public void updateRoleAndPermission(AdminRoleDTO adminRoleDTO) {
        AdminRolePermission rp=new AdminRolePermission();
        rp.setRoleId(adminRoleDTO.getId());
        adminRolePermissionMapper.delete(rp);
        Integer[] permissionIds=adminRoleDTO.getPermissionIds();
        if(permissionIds!=null && permissionIds.length>0){
            List<AdminRolePermission> list=new ArrayList<>();
            for(int roleId:permissionIds){
                AdminRolePermission rolePermission=new AdminRolePermission();
                rolePermission.setRoleId(adminRoleDTO.getId());
                rolePermission.setPermissionId(roleId);
                list.add(rolePermission);
            }
            adminRolePermissionMapper.inserts(list);
        }
    }

    @Override
    public void del(AdminRole adminRole) {
        adminRoleMapper.deleteByPrimaryKey(adminRole);
    }

    @Override
    public void update(AdminRole adminRole) {
        adminRoleMapper.updateByPrimaryKeySelective(adminRole);
    }

    @Override
    public AdminRole selectOne(AdminRole adminRole) {
        return adminRoleMapper.selectByPrimaryKey(adminRole.getId());
    }

    @Override
    public AdminRoleDTO selectAdminRoleAndPermission(AdminRole adminRole) {
        AdminRole role=adminRoleMapper.selectByPrimaryKey(adminRole.getId());
        AdminRoleDTO roleDTO=new AdminRoleDTO();
        roleDTO.setName(role.getName());
        roleDTO.setRemark(role.getRemark());
        roleDTO.setId(role.getId());
        roleDTO.setRoleStatus(role.getRoleStatus());
        Integer[] permissionIds=adminRolePermissionMapper.getPermissionIds(adminRole.getId());
        roleDTO.setPermissionIds(permissionIds);
     return roleDTO;
    }

    @Override
    public List<AdminRole> selectAll() {
        return adminRoleMapper.select(null);
    }
}
